package com.xym.manage.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.abel533.mapper.Mapper;
import com.xym.common.bean.ItemCatData;
import com.xym.common.bean.ItemCatResult;
import com.xym.manage.mapper.ItemCatMapper;
import com.xym.manage.pojo.ItemCat;

@Service
public class ItemCatService extends BaseService<ItemCat> {

	@Autowired
	private ItemCatMapper itemCatMapper;

	/*
	 * public List<ItemCat> queryItemCat(Long parentId) { ItemCat itemCat = new
	 * ItemCat(); itemCat.setParentId(parentId); List list =
	 * itemCatMapper.select(itemCat); return list; }
	 */

	@Override
	public Mapper<ItemCat> getMapper() {
		return this.itemCatMapper;
	}

	
	
	/**
	 * 查询分类封装，前台接口调用
	 *  "data": [
        {
            "u": "/products/1.html",
            "n": "<a href='/products/1.html'>图书、音像、电子书刊</a>",
            "i": [
                {
                    "u": "/products/2.html",
                    "n": "电子书刊",
                    "i": [
                        "/products/3.html|电子书",
                        "/products/4.html|网络原创",
                        "/products/5.html|数字杂志",
                        "/products/6.html|多媒体图书"
                    ]
                },
                {
                    "u": "/products/7.html",
                    "n": "音像",
                    "i": [
                        "/products/8.html|音乐",
                        "/products/9.html|影视",
                        "/products/10.html|教育音像"
                    ]
                },....]}]
	 * @return
	 * 
	 * 运用redis 缓存
	 */
	@Autowired
	private RedisService redisService;
	
	@Autowired
	private PropertiesService propertiesService;
	
	private ObjectMapper mapper = new ObjectMapper();
	
	public ItemCatResult queryApi_ItemCatResult() {
		ItemCatResult itemCatResult = new ItemCatResult();
		/*
		 * List<ItemCatData> itemCats = new ArrayList<ItemCatData>();
		 * ItemCatData itemCatData = new ItemCatData();
		 * itemCatData.setUrl("http://www.jd.com"); itemCatData.setName("fang");
		 * itemCatData.setItems(null); itemCats.add(itemCatData);
		 */

		//String string = redisService.get("itemCatResult");
		//放入redis的key
		String category_key = propertiesService.REDIS_TTEM_CAT_KEY;
		
		String category_ALL = redisService.get(category_key);
		
		try {
			if (category_ALL!=null){
				return mapper.readValue(category_ALL,ItemCatResult.class );
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<ItemCat> list = itemCatMapper.select(null);
		Map<Long, List<ItemCat>> hashMap = new HashMap<>();
		for (ItemCat itemCat : list) {
			if (!hashMap.containsKey(itemCat.getParentId())) {
				hashMap.put(itemCat.getParentId(), new ArrayList<ItemCat>());
			}
			hashMap.get(itemCat.getParentId()).add(itemCat);
		}

		List<ItemCat> itemCatList1 = hashMap.get(0L);
		List<ItemCatData> itemCatDataList1 = new ArrayList<ItemCatData>();

		for (ItemCat itemCat : itemCatList1) {
			ItemCatData itemCatData = new ItemCatData();
			itemCatData.setUrl("/products/" + itemCat.getId() + ".html");
			itemCatData.setName("<a href='/products/" + itemCat.getId()
					+ ".html'>" + itemCat.getName() + "</a>");

			// 分装二级菜单
			List<ItemCat> itemCatList2 = hashMap.get(itemCat.getId());
			ArrayList<ItemCatData> itemCatDataList2 = new ArrayList<ItemCatData>();
			for (ItemCat itemCat2 : itemCatList2) {
				ItemCatData itemCatData2 = new ItemCatData();
				itemCatData2.setUrl("/products/" + itemCat2.getId() + ".html");
				itemCatData2.setName(itemCat2.getName());

				// 封装三级菜单
				List<ItemCat> itemCatList3 = hashMap.get(itemCat2.getId());
				ArrayList<String> itemCatDataList3 = new ArrayList<>();
				for (ItemCat itemCat3 : itemCatList3) {
					itemCatDataList3.add("/products/" + itemCat3.getId()
							+ ".html|" + itemCat3.getName());
				}

				itemCatData2.setItems(itemCatDataList3);
				itemCatDataList2.add(itemCatData2);
			}

			itemCatData.setItems(itemCatDataList2);
			itemCatDataList1.add(itemCatData);
			
			//存放14条数据
			if(itemCatDataList1.size()==14){
				break;
			}
		}

		itemCatResult.setItemCats(itemCatDataList1);
		
		  // 查询数据库后，讲结果放入缓存中
		try {
			redisService.set(category_key, mapper.writeValueAsString(itemCatResult),propertiesService.REDIS_ITEM_CAT_AKK_TIME);
		} catch (JsonProcessingException e) {
		
			e.printStackTrace();
		}
		return itemCatResult;
	}


}
